﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SysCommon
{

    public static class WebApiHelper
    {
        public static string Get(Uri uri, string token)
        {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
            request.ContentType = "application/json";
            request.Method = "GET";
            request.Headers.Add("User-Token", token);
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
            request.UserAgent = "valeb";
            request.Timeout = 1200000;//~ 20分钟

            string result;
            System.Net.HttpWebResponse response = null;
            try
            {
                response = (System.Net.HttpWebResponse)request.GetResponse();
            }
            catch (System.Net.WebException ex)
            {
                response = (System.Net.HttpWebResponse)ex.Response;
            }
            if (response == null)
            {
                throw new Exception("从服务端获取数据失败。可能原因是等待数据返回过久导致timeout!");
            }
            using (var sreader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                result = sreader.ReadToEnd();
                sreader.Close();
                sreader.Dispose();
            }
            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception(result);
            }
            response.Close();
            return result;
        }

        public static string Post(Uri uri, string postjosnStr, string token)
        {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
            request.ContentType = "application/json";
            request.Method = "Post";
            request.Headers.Add("User-Token", token);
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
            request.UserAgent = "valeb";
            request.KeepAlive = true;
            request.ServicePoint.Expect100Continue = false;
            request.Timeout = 1800000;//~ 半小时

            string result;
            byte[] byteArray = Encoding.UTF8.GetBytes(postjosnStr);
            request.ContentLength = byteArray.Length;
            using (System.IO.Stream inputstream = request.GetRequestStream())
            {
                inputstream.Write(byteArray, 0, byteArray.Length);
                System.Net.HttpWebResponse response = null;
                try
                {
                    response = (System.Net.HttpWebResponse)request.GetResponse();
                }
                catch (System.Net.WebException ex)
                {
                    response = (System.Net.HttpWebResponse)ex.Response;
                }
                if (response == null)
                {
                    throw new Exception("从服务端获取数据失败。可能原因是等待数据返回过久导致timeout!");
                }

                using (var sreader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    result = sreader.ReadToEnd();
                    sreader.Close();
                    sreader.Dispose();
                }
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception(result);
                }
                response.Close();

                inputstream.Close();
                inputstream.Dispose();
            }
            return result;
        }
        /*
        private bool TryHttpRequest(Uri uri, out string retrunValue, string method = "GET", string contentType = "application/json", string postjosnval = null)
        {
            System.Net.HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(uri);
            request.ContentType = contentType;
            request.Method = postjosnval;
            System.IO.Stream inputstream = null;
            if (string.IsNullOrEmpty(postjosnval) == false)
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(postjosnval);
                inputstream = request.GetRequestStream();
                inputstream.Write(byteArray, 0, byteArray.Length);
            }
            System.Net.WebResponse response = request.GetResponse();
            using (System.IO.StreamReader sreader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                string aa = sreader.ReadToEnd();
                sreader.Close();
                sreader.Dispose();
            }
            if (null != inputstream)
            {
                inputstream.Close();
                inputstream.Dispose();
            }
        }*/
    }

    //public class WebApiUriHelper
    //{
    //private static string _ServiceUri = string.Empty;

    //public static string GetUrlServerPath(string appName)
    //{
    //    if (string.IsNullOrEmpty(_ServiceUri))
    //    {
    //        System.Reflection.Assembly assembly;
    //        System.Configuration.ExeConfigurationFileMap map = new System.Configuration.ExeConfigurationFileMap();
    //        assembly = System.Reflection.Assembly.GetCallingAssembly();
    //        map.ExeConfigFilename = Path.Combine(Path.GetDirectoryName(assembly.CodeBase).Substring("file:\\".Length), "CallCenter.config");
    //        System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
    //        _ServiceUri = config.AppSettings.Settings["ApiService"].Value;
    //    }
    //    return _ServiceUri;
    //    //return "http://192.168.1.66:8080";
    //    //return "http://localhost:8080";

    //}
    //}
}
